package com.lys.msmservice.controller;

import com.lys.commonutils.R;
import com.lys.commonutils.RandomUtil;
import com.lys.msmservice.service.MsmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @author Lyyyys
 * @version 1.0
 * @date 2021/6/17 20:53
 */
@RestController
@RequestMapping("/edumsm/msm")
@CrossOrigin
public class MsmController {

    @Autowired
    private MsmService msmService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @GetMapping("send/{phone}")
    public R sendMsm(@PathVariable String phone){
        //1.如果redis中存在验证码，就直接返回
        String code = redisTemplate.opsForValue().get(phone);
        if(!StringUtils.isEmpty(code)){
            return R.ok();
        }
        //2.如果redis中获取不到，就生成一个验证码
        //生成随机值，通过控制台输出打印后去进行注册账号
        code = RandomUtil.getFourBitRandom();
        System.out.println("您正在申请的验证码为："+code + ",5分钟之内有效！");
        redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
        return R.ok();

    }

}
